package cn.com.cetccst.uicomponents.binding.viewpage2;

import android.util.Log;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.MarginPageTransformer;
import androidx.viewpager2.widget.ViewPager2;

import java.util.List;

public class ViewPagerBindingAdapter {

    @BindingAdapter(value = {"adapter", "submitList","currentItem"}, requireAll = false)
    public static void bindPagerAdapter(ViewPager2 viewPager, ListAdapter adapter, List list, int currentItem) {
        if (viewPager.getAdapter() == null || viewPager.getAdapter() != adapter) {
            viewPager.setAdapter(adapter);
        }
        if(adapter!=null)
            adapter.submitList(list);

        viewPager.setCurrentItem(currentItem);
    }

    @BindingAdapter(value = "pageChangeListener")
    public static void bindPagerChangeListener(ViewPager2 viewPager, ViewPager2.OnPageChangeCallback pageChangeListener) {
        if (pageChangeListener != null) {
            viewPager.registerOnPageChangeCallback(pageChangeListener);
        }
    }

    @BindingAdapter(value = "pageMargin")
    public static void bindPageMargin(ViewPager2 viewPager, int pageMargin) {
        MarginPageTransformer pageTransformer = new MarginPageTransformer(pageMargin);
        viewPager.setPageTransformer(pageTransformer);

    }

}
